<?php

class entries_model extends CI_Model
{
	var $title = '';
	var $content = '';
	var $date = '';

	public function __construct()
	{
		parent::__construct();
	}

	// 全部查询查询
	public function get($offset, $num)
	{
		//$str_sql  = "select * from entries order by $order limit $offset,$num";
		return array(
			'total' => $this->db->count_all('entries'),
			'res' => $this->db->get('entries',$num, $offset)->result_array()
		);
	}

	// 选择查询
	public function get_entries($id)
	{
		return $this->db->get_where('entries',array('eId'=>$id))->row_array();
	}

	// 模糊搜索
	public function fuzzy_search($arr, $offset,$num)
	{
		$i = 1;
		foreach($arr as $k=>$v){
			if($v){
				if($i == 1){
					$this->db->like($k, $v);
				}else{
					$this->db->or_like($k, $v);
				}
			}
			$i++;
		}
		$result = $this->db->get('entries',$num, $offset)->result_array();
		return $result;
	}

	public function count($arr = array())
	{
		$i = 1;
		$this->db->select('count(*) as countNum');
		foreach($arr as $k=>$v){
			if($v){
				if($i == 1){
					$this->db->like($k, $v);
				}else{
					$this->db->or_like($k, $v);
				}
			}
			$i++;
		}
		$count =  $this->db->get('entries')->row_array();

		return $count['countNum'];
	}

	// 插入数据
	public function insert($data)
	{
		$this->db->insert('entries',$data);
		return $this->db->insert_id() ; // 返回的是ID
	}

	// 更新数据
	public function update($id, $data)
	{
		return $this->db->update('entries',$data,array('eId'=>$id));
	}

	// 删除数据
	public function delete($id)
	{
		$res = $this->db->delete('entries',array('eId'=>$id));
		return $res;
	}

	// 查询某个用户发表的全部文章
	public function getAll_of_someOne($id)
	{
		return $this->db->get_where('entries',array('userId'=>$id))->result_array();
	}

	// 根据用户ID删除其下的所有文章
	public function delAll_of_someOne($id)
	{
		return $this->db->delete('entries', array('userId' => $id));
	}
}